<!DOCTYPE html>
<html>

<head>

    {include file="Public/header"}
    <link href="__STATIC__/layout/css/gyq.css" rel="stylesheet">
    <link rel="stylesheet" href="__STATIC__/layout/css/demo.css" type="text/css">
    <style>
        .radio-inline input[type=radio] {
            margin-top: 4px
        }

        .borRed {
            border: 1px solid red;
            padding: 4px;
        }

        .textBorder {
            border-color: red
        }
    </style>
</head>

<body style="color:#333;padding:3px;" class="x_sorll">
<!-- 标题 -->
<div class="page-heade">
    <h2 class="title_color text-center">TI费用核销申请-查询</h2>
</div>
<!-- 标题 -->
<div>
    <div action="" class="form-inline">
        <table class="table table-bordered bud_hei" style="margin-top:0">
            <tr>
                <td><span class="font-bold">{$data.jxs_title}({$data.jxs_sn})</span>
                    <a class="btn btn-warning" type="button" href="{:url('changejxsafter',['id'=>$data.jxs_id])}">点击返回上一步</a>
                    <a href="{:url('changejxs')}" class="btn-info btn">点击更换经销商</a>
                </td>
            </tr>
            <tr>
                <td>
                    <a href="{:url('',['tiids'=>request()->param('tiids'),'type'=>0])}" class="btn btn-primary {if request()->param('type')==0}btn-outline{/if}">可核销</a>
                    <a href="{:url('',['tiids'=>request()->param('tiids'),'type'=>1])}" class="btn btn-primary {if request()->param('type')==1}btn-outline{/if}">核销中</a>
                    <a href="{:url('',['tiids'=>request()->param('tiids'),'type'=>2])}" class="btn btn-primary {if request()->param('type')==2}btn-outline{/if}">不予核销</a>
                    <a href="{:url('',['tiids'=>request()->param('tiids'),'type'=>3])}" class="btn btn-primary {if request()->param('type')==3}btn-outline{/if}">未反馈</a>
                    <a href="{:url('',['tiids'=>request()->param('tiids'),'type'=>4])}" class="btn btn-primary {if request()->param('type')==4}btn-outline{/if}">未核查</a>
                    <a href="{:url('',['tiids'=>request()->param('tiids'),'type'=>5])}" class="btn btn-primary {if request()->param('type')==5}btn-outline{/if}">已锁定</a>
                </td>
            </tr>
            <tr>
                <td class="colorRed">温馨提示：一旦核销，原申请整单都不允许再做异动、取消、反馈。谢谢</td>
            </tr>
            <tr>
                <td class="font-bold">请选择下列要核销的记录后，点击"生成核销面单预览"按钮后即可核销。</td>
            </tr>
        </table>
        <table class="table table-bordered bud_hei text-center" id="table1">
            <tr class="info">
                <th class="text-center" rowspan="2">序号</th>
                <th class="text-center" rowspan="2">
                    <input type="checkbox" id="checkAll" class="form-control">
                </th>
                <th class="text-center" rowspan="2">申请人</th>
                <th  class="text-center" rowspan="2">原申请文件</th>
                <th  class="text-center" rowspan="2">申请主题</th>
                <th class="text-center" rowspan="2">申请类型</th>
                <th class="text-center" rowspan="2">申请时间
                </th>
                <th class="text-center" rowspan="2">网点</th>
                <th class="text-center" rowspan="2">三级费用科目</th>
                <th class="text-center" rowspan="2">产品纬度</th>
                <th class="text-center" colspan="3">实际投入</th>
                <th class="text-center" colspan="8"></th>
                <th class="text-center" rowspan="2">余额不予核销</th>
                <th class="text-center" rowspan="2">支付方式</th>
                <th class="text-center" rowspan="2">备注</th>
                <th class="text-center" rowspan="2">操作</th>
            </tr>
            <tr class="info">
                <th class="text-center">实际投入金额</th>
                <th class="text-center">已核销金额</th>
                <th class="text-center">未核销金额</th>
                <th class="text-center">未税核销金额</th>
                <th class="text-center">
                    有无发票<br>
                    <button type="button" class="btn-info" id="is_fap">批量设置</button>
                </th>
                <th class="text-center">
                    发票号<span class="colorRed font-bold">?</span><br>
                    <button type="button" class=" btn-info" id="setFpNum">批量设置</button>
                    <span class="helpText">帮</span>
                </th>
                <th class="text-center">
                    是否增值税<br>(可抵扣)<span class="colorRed font-bold">?</span>
                </th>
                <th class="text-center">
                    税率<span class="colorRed font-bold">?</span>
                </th>
                <th class="text-center">税金</th>
                <th class="text-center">含税核销金额</th>
                <th class="text-center">实际投入金额</th>
            </tr>
            {volist name="data.list" id="row" key="k"}
            <tr class="data">
                <td>{$k}</td>
                <td><input type="checkbox" name="items" class="form-control" value="{$row.ti_id}"></td>
                <td>{:db('employee')->where('id',$row.cuser)->value('em_name')}</td>
                <td><a href="{:url('view',['id'=>$data.id])}">{$data.sn}</a></td>
                <td>{$row.ti_title}</td>
                <td>{$row.cate_name}</td>
                <td>{:date('Y-m-d',$row.ti_ctime)}</td>
                <td>{$row.net_num}</td>
                <td>{$row.feetype}</td>
                <td>{$row.product_name}</td>
                <td class="ObjectNum">{$row.shijitourujine}</td>
                <td class="ObjectNum">{$row.hx_yihexiao}</td>
                <td class="ObjectNum">{$row.weihexiaojine}</td>
                <td><input name="weishuihexiaojine" type="text" class="form-control" value="{$row.weihexiaojine}" style="min-width: 100px;width: 100px"></td>
                <td>
                    <div class="form-group radioDiv">
                        <label class="radio-inline">
                            <input type="radio" class="youwufapiao" name="ionse{$k}" value="1">有
                        </label>
                        <label class="radio-inline">
                            <input type="radio" class="youwufapiao" name="ionse{$k}" value="0">无
                        </label>
                    </div>
                </td>
                <td><input type="text" name="fapiaohao" class="form-control"></td>
                <td>
                    <div class="form-group radioDiv">
                        <label class="radio-inline">
                            <input type="radio" class="shizengzhishui" name="isVAT{$k}" value="1">是
                        </label>
                        <label class="radio-inline">
                            <input type="radio" class="shizengzhishui" name="isVAT{$k}" value="0" checked>否
                        </label>
                    </div>
                </td>
                <td><input type="text" style="min-width: 60px;width: 60px;" class="form-control" name="shuilv" value="0.00">%</td>
                <td><input type="text" style="min-width: 80px;width: 80px;" class="form-control" name="shuijin" value="0.00"></td>
                <td><input type="text" class="form-control" placeholder="" name="hanshuihexiaojine" value="{$row.weihexiaojine}" readonly></td>
                <td><input type="text" class="form-control" placeholder="" name="shijitourujine" value="{$row.weihexiaojine}" readonly></td>
                <td><input type="checkbox" name="yuebuhexiao" class="form-control" value="1"></td>
                <td><select name="zhifufangshi"  class="form-control">
                    {volist name="paymethod" id="row2"}
                    <option value="{$row2.id}" {if $row.paymethod==$row2.id}selected{/if}>{$row2.title}</option>
                    {/volist}
                </select></td>
                <td><input type="text" name="beizhu" class="form-control"></td>
                <td>
                    <button type="button" class="btn-info buyuhexiao" data-link="{:url('buyuhexiao',['id'=>$row.id])}" data-shijitouru="{$row.fb_shijitourufeiyong}" data-yihexiao="{$row.hx_yihexiao}">本条不予核销</button>
                </td>
            </tr>
            {/volist}

        </table>
        <div style="text-align: center;height:30px;line-height:30px;margin-bottom:10px;">
            <a href="javascript:;" id="jiazaigengduo">加载更多</a>
        </div>
        <table class="table-bordered table">
            <tr>
                <td rowspan="3" class="text-center" style="min-width: 120px;width: 120px">备注:</td>
                <td><textarea name="brief" id="brief" class="form-control" style="width: 60%;overflow: auto;"></textarea><span
                        style="vertical-align: bottom;">(500)</span></td>
            </tr>
        </table>
        <p class="colorRed">温馨提示：<br>
            1.如果有明细单不需要核销，请在该费用明细最后面点击："本条不予核销"按钮。<br>
            2.如核销一部分，所剩费用余额不需要核销,请勾选："余额不予核销"。
        </p>
        <div style="height:103px;"></div>
        {if request()->param('type')=='0'||request()->param('type')==null}
        <div class="footer fixed_full text-center" style="height: auto;border:0;padding: 0;">
            <table class="table-bordered table text-center" style="margin:0;padding: 0;background: #f3f3f4">
                <tr>
                    <td>未税金额合计：<span class="colorRed font-bold ObjectNum" id="notax">0</span>元；
                        <button type="button" class="btn-primary btn" id="modify_notax">修改未税金额</button>
                    </td>
                    <td>税金合计：<span class="colorRed font-bold ObjectNum" id="shuijinheji">0</span>元；
                        <button type="button" class="btn-primary btn" id="setFpInfo">设置发票信息</button>
                    </td>
                    <td rowspan="2">
                        <button type="button" class="btn-primary btn" id="create_verify_yl">生成核销申请预览</button>
                    </td>
                </tr>
                <tr>
                    <td>含税金额合计：<span class="colorRed font-bold ObjectNum" id="tax">0</span>元；
                        <button type="button" class="btn-primary btn" id="modify_tax">修改含税金额</button>
                    </td>
                    <td><input name="items1" type="checkbox" id="checkboxAll1">设置余额不予核销</td>
                </tr>
            </table>
        </div>
        {/if}
    </div>
</div>
{include file="Public/footer"}

<!-- <script src="js/plugins/metisMenu/jquery.metisMenu.js"></script> -->
<script src="__STATIC__/layout/js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<script src="__STATIC__/layout/js/plugins/layer/laydate/laydate.js"></script>
<script src="__STATIC__/layout/js/common.js"></script>
<script>

    //计算合计
    function sumheji(o){
        if($(o).closest('tr').find('[name=items]').prop('checked')==true){
            //计算未税金额合计
            var heji = 0
            var heji2 = 0
            var heji3 = 0
            $('[name=items]:checked').each(function () {
                heji += parseFloat($(this).closest('tr').find('[name=weishuihexiaojine]').val())
                heji2 += parseFloat($(this).closest('tr').find('[name=hanshuihexiaojine]').val())
                heji3 += parseFloat($(this).closest('tr').find('[name=shuijin]').val())
            })
            $('#notax').html(heji)
            $('#tax').html(heji2)
            $('#shuijinheji').html(heji3)
        }
    }

    $(document).ready(function () {
        //加载更多
        page = 2;
        $('#jiazaigengduo').on('click',function () {
            var tiids = '{:request()->param("tiids")}'
            $.post('{:url("hexiaomorebyzd")}',{page:page,tiids:'{:request()->param("tiids")}',type:'{:request()->param("type")}'},function (data) {
                if(data==''){
                    $('#jiazaigengduo').html('没有更多了')
                }else{
                    $('#table1').append(data)
                    page++;
                }
            })
        })
        //设置余额不予核销
        $('#checkboxAll1').on('click',function () {
            var _this = this
            if($('[name=items]:checked').length==0){
                swal('请至少选择一条记录')
                return false
            }
            if($(this).prop('checked')==false){
                $('[name=items]:checked').each(function (i,o) {
                    $(o).closest('tr').find('[name=yuebuhexiao]').prop('checked',false)
                })
                return true
            }else{

                swal({
                    title: "您确定设置选定的余额不再核销吗？如确定则该余额自动释放会预算池。请确认！",
                    text: "",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确定",
                    closeOnConfirm: false
                }, function (isConfirm) {

                    if(isConfirm){
                        $('[name=items]:checked').each(function (i,o) {
                            $(o).closest('tr').find('[name=yuebuhexiao]').prop('checked',true)
                        })
                        $(_this).prop('checked',true)
                    }else{
                        $('[name=items]:checked').each(function (i,o) {
                            $(o).closest('tr').find('[name=yuebuhexiao]').prop('checked',false)
                        })
                        $(_this).prop('checked',false)
                    }
                    swal.close()
                })

            }
            return false
        })
        
        //設置本條不予核銷
        $('.buyuhexiao').on('click',function () {
            var shijitouru = $(this).data('shijitouru')
            var yihexiao = $(this).data('yihexiao')
            var weihexiao = parseFloat(shijitouru)-parseFloat(yihexiao)
            var link = $(this).data('link')
            var str = "实际投入金额为："+shijitouru+',已核销金额为：'+yihexiao+',未核销金额为：'+weihexiao+',确定后不予核销金额为：'+weihexiao+',请确认是否不予核销该金额再点击确定。'
            swal({
                title: "您确定操作吗",
                text: str,
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                closeOnConfirm: false
            }, function (isConfirm) {
                if(isConfirm){
                    $.ajax({
                        'type':'post',
                        'url':link,
                        'dataType':'json',
                        'success':function (data) {
                            if(data.success){
                                swal({
                                    title:data.msg,
                                    type:'success'
                                },function () {
                                    location.reload()
                                })
                            }else
                                swal(data.msg, "", "error");
                        }
                    })
                }
            });

            return false
        })

        /*进入按网点反馈页面*/
        $("#toNetFk").click(function () {
            var _$this = this
            swal({
                    title: "",
                    text: "页面跳转后，当前页面已编辑的数据将不被保存，您确定跳转吗？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    closeOnConfirm: false,
                    closeOnCancel: false
                },
                function (isConfirm) {
                    if (isConfirm) {
                        // console.log(_$this);
                        swal("", "", "success");
                        location.href = "ROIjcyj.html"
                    } else {
                        swal("取消！", "", "error");
                    }
                });
        })
        /*设置checkbox*/
        /*checkbox选择*/
        $("#checkAll").click(function () {

            $("[name=items]:checkbox").prop("checked", $(this).prop("checked"));
            if ($("[name=items]:checkbox").prop("checked") != true) {
                $("[name=items]:checkbox").parents("tr").css("backgroundColor", "#fff")
            } else {
                $("[name=items]:checkbox").parents("tr").css("backgroundColor", "#f8f800")
            }

            //重新计算三个合计

            //计算未税金额合计
            var heji = 0
            $('[name=items]:checked').each(function () {
                heji += parseFloat($(this).closest('tr').find('[name=weishuihexiaojine]').val())
            })
            $('#notax').html(heji)

            //计算未税金额合计
            heji = 0
            $('[name=items]:checked').each(function () {
                heji += parseFloat($(this).closest('tr').find('[name=hanshuihexiaojine]').val())
            })
            $('#tax').html(heji)

            //计算未税金额合计
            heji = 0
            $('[name=items]:checked').each(function () {
                heji += parseFloat($(this).closest('tr').find('[name=shuijin]').val())
            })
            $('#shuijinheji').html(heji)
        })
        //选择
        $("[name=items]:checkbox").click(function () {
            //定义一个临时变量，避免重复使用同一个选择器选择页面中的元素，提高程序效率
            var $tmp = $("[name=items]:checkbox");
            if ($(this).prop("checked") != true) {
                // console.log($(this))
                $(this).parent().parent().css("backgroundColor", "#fff")
            } else {
                $(this).parent().parent().css("backgroundColor", "#f8f800")
            }
            //用filter()方法筛选出选中的复选框，并直接给CheckedAll赋值
            $("#checkAll").prop('checked', $tmp.length == $tmp.filter(':checked').length);

            //重新计算三个合计

            //计算未税金额合计
            var heji = 0
            $('[name=items]:checked').each(function () {
                heji += parseFloat($(this).closest('tr').find('[name=weishuihexiaojine]').val())
            })
            $('#notax').html(heji)

            //计算未税金额合计
            heji = 0
            $('[name=items]:checked').each(function () {
                heji += parseFloat($(this).closest('tr').find('[name=hanshuihexiaojine]').val())
            })
            $('#tax').html(heji)

            //计算未税金额合计
            heji = 0
            $('[name=items]:checked').each(function () {
                heji += parseFloat($(this).closest('tr').find('[name=shuijin]').val())
            })
            $('#shuijinheji').html(heji)

        })
        /*设置有无发票*/
        $("#is_fap").click(function () {
            var str = '<div method="get" class="form-inline" style="margin:10px"><table class="table-bordered table"><tr><th class="info text-center" colspan="2">批量设置有无发票</th></tr><tr><td style="min-width:120px;width:120px;" class="text-center">有无发票<span class="colorRed">*</span></td><td><label class="radio-inline"><input type="radio" name="youwufapiao" value="1" >有</label><label class="radio-inline"><input type="radio" name="youwufapiao" value="0">无</label></td></tr></table> <div class="row sub_btn" style="width:100%;text-align:center;padding: 20px 0"><input type="submit" class="btn btn-success" value="确定" id="queding"><input type="button" class="btn btn-warning" value="返回" id="cancelBack" style="margin-left:20px"></div></div>'
            layer.open({
                type: 1,
                title: "",
                skin: 'layui-layer-rim', //加上边框
                area: ['500px', '300px'], //宽高
                content: str
            });
            var cancelBack = document.getElementById('cancelBack')
            cancelBack.onclick = function () {
                layer.closeAll();
            }
            //确定
            $('#queding').on('click',function () {
                var youwufapiao = $('[name=youwufapiao]:checked').val()
                if(typeof youwufapiao=='undefined'){
                    swal('请选择有无发票')
                    return false
                }
                $('[name=items]:checked').each(function () {
                    $(this).closest('tr').find('.youwufapiao').prop('checked',function (i,val) {
                        if($(this).val()==youwufapiao)
                            return true
                        else
                            return false
                    })
                })
                layer.closeAll();
            })
        })
        /*修改未税金额*/
        $("#modify_notax").click(function () {
            var notax = $('#notax').text()
            var str = '<div method="get" class="form-inline" style="margin:10px"><table class="table-bordered table"><tr><th class="info text-center" colspan="2">TI核销修改未税金额</th></tr><tr><td style="min-width:140px;width:140px;" class="text-center">原未税金额(元)</td><td><span class="ObjectNum">'+notax+'</span></td></tr><tr><td style="min-width:140px;width:140px;" class="text-center">修改后未税金额(元)</td><td><input type="text" name="" id="notaxxg" class="form-control" style="min-width:100px;width:100px;"/></td></tr></table> <div class="row sub_btn" style="width:100%;text-align:center;padding: 20px 0"><input type="submit" class="btn btn-success" value="确认提交" id="querentijiao"><input type="button" class="btn btn-warning" value="关闭返回" id="cancelBack" style="margin-left:20px"></div></div>'
            layer.open({
                type: 1,
                title: "TI核销修改未税金额",
                skin: 'layui-layer-rim', //加上边框
                area: ['500px', '300px'], //宽高
                content: str
            });
            var cancelBack = document.getElementById('cancelBack')
            cancelBack.onclick = function () {
                layer.closeAll();
            }

            //确认提交
            $('#querentijiao').on('click',function () {
                $('#notax').html($('#notaxxg').val())
                layer.closeAll();
            })
        })
        /*修改含税金额*/
        $("#modify_tax").click(function () {
            var tax = $('#tax').text()
            var str = '<div method="get" class="form-inline" style="margin:10px"><table class="table-bordered table"><tr><th class="info text-center" colspan="2">TI核销修改含税金额</th></tr><tr><td style="min-width:140px;width:140px;" class="text-center">原含税金额(元)</td><td><span class="ObjectNum">'+tax+'</span></td></tr><tr><td style="min-width:140px;width:140px;" class="text-center">修改后含税金额(元)</td><td><input type="text" id="notaxxg" name="" class="form-control" style="min-width:100px;width:100px;"/></td></tr></table> <div class="row sub_btn" style="width:100%;text-align:center;padding: 20px 0"><input type="submit" class="btn btn-success" value="确认提交" id="querentijiao"><input type="button" class="btn btn-warning" value="关闭返回" id="cancelBack" style="margin-left:20px"></div></div>'
            layer.open({
                type: 1,
                title: "TI核销修改含税金额",
                skin: 'layui-layer-rim', //加上边框
                area: ['500px', '300px'], //宽高
                content: str
            });
            var cancelBack = document.getElementById('cancelBack')
            cancelBack.onclick = function () {
                layer.closeAll();
            }
            //确认提交
            $('#querentijiao').on('click',function () {
                $('#tax').html($('#notaxxg').val())
                layer.closeAll();
            })
        })
        /*批量设置发票号*/
        $("#setFpNum").click(function () {
            var str = '<div method="get" class="form-inline" style="margin:10px"><table class="table-bordered table"><tr><th class="info text-center" colspan="2">批量设置发票号</th></tr><tr><td style="min-width:120px;width:120px;" class="text-center">发票号<span class="colorRed">*</span>：</td><td><input type="text" name="fapiaohao2" class="form-control"/></td></tr></table> <div class="row sub_btn" style="width:100%;text-align:center;padding: 20px 0"><input type="submit" class="btn btn-success" value="确定" id="queding"><input type="button" class="btn btn-warning" value="返回" id="cancelBack" style="margin-left:20px"></div></div>'
            layer.open({
                type: 1,
                title: "",
                skin: 'layui-layer-rim', //加上边框
                area: ['500px', '300px'], //宽高
                content: str
            });
            var cancelBack = document.getElementById('cancelBack')
            cancelBack.onclick = function () {
                layer.closeAll();
            }

            //确定
            $('#queding').on('click',function () {

                var youwufapiao = $('[name=fapiaohao2]').val()
                if(youwufapiao==''){
                    swal('请输入发票号')
                    return false
                }
                if( $('[name=items]:checked').length==0){
                    swal('请至少选择一行数据')
                    return false
                }
                $('[name=items]:checked').each(function () {
                        $(this).closest('tr').find('[name=fapiaohao]').val(youwufapiao)
                })
                layer.closeAll();
            })
        })
        /*设置发票信息*/
        $("#setFpInfo").click(function () {

            layer.open({
                type: 2,
                skin: 'layui-layer-rim', //加上边框
                area: ['660px', '500px'], //宽高
                content: '{:url("setInvoice")}'
            });
        })
        /*生成核销申请预览*/
        $("#create_verify_yl").click(function () {
            var bool = true

            if($('input:checkbox[name=items]:checked').length<=0){
                swal("", "请至少选择一条数据！", "error");
                return false
            }

            $('input:checkbox[name=items]:checked').each(function () {
                var tr = $(this).closest('tr')
                var _this

                //验证未税核销金额
                if(''== tr.find('[name=weishuihexiaojine]').val()||parseFloat(tr.find('[name=weishuihexiaojine]').val())<=0){
                    swal("", "请将红色框内的数据填写完整！", "error");
                    tr.find('[name=weishuihexiaojine]').addClass("textBorder")
                    bool = false
                    return false
                }else {
                    tr.find('[name=weishuihexiaojine]').removeClass("textBorder")

                }

                //验证有无发票
                _this = tr.find('input:radio.youwufapiao:checked')
                if('undefined'==typeof _this.val()){
                    swal("", "请在红色框内的做出选择！", "error");
                    tr.find('input:radio.youwufapiao').closest(".radioDiv").addClass("borRed");
                    bool = false
                    return false
                }else {
                    tr.find('input:radio.youwufapiao').closest(".radioDiv").removeClass("borRed");

                    if(_this.val()==1){
                        //验证发票号
                        var _this = tr.find("input[type='text'][name='fapiaohao']");
                        if (_this.val() == '') {
                            swal("", "请将红色框内的数据填写完整！", "error");
                            _this.addClass("textBorder")
                            bool =false
                            return false
                        } else {
                            _this.removeClass("textBorder")
                        }
                    }

                }

                //验证是否增值税
                if('undefined'==typeof tr.find('input:radio.shizengzhishui:checked').val()){
                    swal("", "请在红色框内的做出选择！", "error");
                    tr.find('input:radio.shizengzhishui').closest(".radioDiv").addClass("borRed");
                    bool = false
                    return false
                }else {
                    tr.find('input:radio.shizengzhishui').closest(".radioDiv").removeClass("borRed");
                }


                //验证税率
                var _this = tr.find("input[type='text'][name='shuilv']");
                if (_this.val() == '') {
                    swal("", "请将红色框内的数据填写完整！", "error");
                    _this.addClass("textBorder")
                    bool =false
                    return false
                } else {
                    _this.removeClass("textBorder")
                }

                //验证税金
                var _this = tr.find("input[type='text'][name='shuijin']");
                if (_this.val() == '') {
                    swal("", "请将红色框内的数据填写完整！", "error");
                    _this.addClass("textBorder")
                    bool =false
                    return false
                } else {
                    _this.removeClass("textBorder")
                }

                //含税核销金额
                var _this = tr.find("input[type='text'][name='hanshuihexiaojine']");
                if (_this.val() == '') {
                    swal("", "请将红色框内的数据填写完整！", "error");
                    _this.addClass("textBorder")
                    bool =false
                    return false
                } else {
                    _this.removeClass("textBorder")
                }

                //实际投入金额
                var _this = tr.find("input[type='text'][name='shijitourujine']");
                if (_this.val() == '') {
                    swal("", "请将红色框内的数据填写完整！", "error");
                    _this.addClass("textBorder")
                    bool =false
                    return false
                } else {
                    _this.removeClass("textBorder")
                }

                //活动开始日期
                var _this = tr.find("input[type='text'][name='huodongkaishiriqi']");
                if (_this.val() == '') {
                    swal("", "请将红色框内的数据填写完整！", "error");
                    _this.addClass("textBorder")
                    bool =false
                    return false
                } else {
                    _this.removeClass("textBorder")
                }

                //生成核销预览
            })

            if(bool==false){
                return false
            }

            var data = []
            $('input:checkbox[name=items]:checked').each(function () {
                var tr = $(this).closest('tr')
                var weishuihexiaojine = tr.find('[name=weishuihexiaojine]').val()
                var youwufapiao = tr.find('[name^=ionse]:checked').val()
                var fapiaohao = tr.find('[name=fapiaohao]').val()
                var shizengzhishui = tr.find('[name^=isVAT]:checked').val()
                var shuilv = tr.find('[name=shuilv]').val()
                var shuijin = tr.find('[name=shuijin]').val()
                var hanshuihexiaojine = tr.find('[name=hanshuihexiaojine]').val()
                var shijitourujine = tr.find('[name=shijitourujine]').val()
                var yuebuhexiao = tr.find('[name=yuebuhexiao]:checked').val()
                yuebuhexiao = typeof yuebuhexiao=='undefined'?0:1;
                var huodongkaishiriqi = tr.find('[name=huodongkaishiriqi]').val()
                var zhifufangshi = tr.find('[name=zhifufangshi]').val()
                var beizhu = tr.find('[name=beizhu]').val()
                var id = $(this).val()
                data.push({
                    ti_id:id,
                    weishuihexiaojine:weishuihexiaojine,
                    youwufapiao:youwufapiao,
                    fapiaohao:fapiaohao,
                    shizengzhishui:shizengzhishui,
                    shuilv:shuilv,
                    shuijin:shuijin,
                    hanshuihexiaojine:hanshuihexiaojine,
                    shijitourujine:shijitourujine,
                    yuebuhexiao:yuebuhexiao,
                    huodongkaishiriqi:huodongkaishiriqi,
                    zhifufangshi:zhifufangshi,
                    beizhu:beizhu
                })
            })
            var notax = $('#notax').text()
            var tax = $('#tax').text()
            var shuijinheji = $('#shuijinheji').text()
            $.post('{:url("makehxpreviewbyzd")}',
                {data:data,notax:notax,tax:tax,shuijinheji:shuijinheji,id:"{:request()->param('id')}"},
                function (data) {
                    if(data.success){
                        swal({
                            title:data.msg,
                            type:'success'
                        },function () {
                            location.href='{:url("hxpreview",["id"=>request()->param("id")])}?hx_id='+data.hx_id
                        })
                    }else
                        swal(data.msg, "", "error");
            },'json')

        });

        var $ObjectNum = $(".ObjectNum");
        toNum($ObjectNum);


        //输入框获得焦点时文本自动选中
        $('input[type=text]').on('focus',function () {
            this.select();
        })

        //输入税率自动计算税金等字段
        $('[name=shuilv]').on('blur',function () {
            var shuilv = $(this).val()
            if(!/^\d+(\.\d+)?$/.test(shuilv)){
                swal('请输入非负浮点数')
                $(this).val('')
                return false
            }

            shuilv = parseFloat(shuilv)
            var weishui = $(this).closest('tr').find('[name=weishuihexiaojine]').val()
            weishui = parseFloat(weishui)


            var shuijin = (weishui*(shuilv/100)).toFixed(2)
            var shuijin = parseFloat(shuijin)


            var shijitouru  //实际投入金额
            var  zengshishui = $(this).closest('tr').find('input.shizengzhishui:checked').val()
            if(zengshishui==1)
                shijitouru = weishui
            else
                shijitouru = (weishui+shuijin).toFixed(2)

            var td = $(this).closest('tr').find('[name=weishuihexiaojine]').closest('td').prev('td').html()
            td = td.replace(',','')
            td = parseFloat(td)
            if(shijitouru>td){
                swal('','本次核销实际投入金额'+(weishui+shuijin).toFixed(2)+',不能大于未核销金额'+td,'error')
                $(this).val('')
                return false
            }



            $(this).closest('tr').find('[name=shuijin]').val(shuijin) //税金
            $(this).closest('tr').find('[name=hanshuihexiaojine]').val((weishui+shuijin).toFixed(2)) //含税核销金额
            $(this).closest('tr').find('[name=shijitourujine]').val(shijitouru) //实际投入金额

            sumheji(this)
        })

        //切换是否增值税自动计算税金等字段
        $('.shizengzhishui').on('click',function () {
            var  zengshishui = $(this).val()
            var shuilv = $(this).closest('tr').find('[name=shuilv]').val()
            shuilv = parseFloat(shuilv)
            var weishui = $(this).closest('tr').find('[name=weishuihexiaojine]').val()
            weishui = parseFloat(weishui)

            var shuijin = (weishui*(shuilv/100)).toFixed(2)
            var shuijin = parseFloat(shuijin)

            var shijitouru  //实际投入金额
            if(zengshishui==1)
                shijitouru = weishui
            else
                shijitouru = (weishui+shuijin).toFixed(2)

            var td = $(this).closest('tr').find('[name=weishuihexiaojine]').closest('td').prev('td').html()
            td = td.replace(',','')
            td = parseFloat(td)
            if(shijitouru>td){
                swal('','本次核销实际投入金额'+(weishui+shuijin).toFixed(2)+',不能大于未核销金额'+td,'error')
                return false
            }

            $(this).closest('tr').find('[name=shuijin]').val(shuijin) //税金
            $(this).closest('tr').find('[name=hanshuihexiaojine]').val((weishui+shuijin).toFixed(2)) //含税核销金额
            $(this).closest('tr').find('[name=shijitourujine]').val(shijitouru) //实际投入金额

            sumheji(this)
        })

        //输入税金自动折算税率等字段
        $('[name=shuijin]').on('blur',function () {
            var shuijin = $(this).val()
            if(!/^\d+(\.\d+)?$/.test(shuijin)){
                swal('请输入非负浮点数')
                $(this).val('')
                return false
            }
            shuijin = parseFloat(shuijin)

            var weishui = $(this).closest('tr').find('[name=weishuihexiaojine]').val()
            weishui = parseFloat(weishui)

            var shuilv = parseFloat((shuijin/weishui*100).toFixed(2))

            var shijitouru  //实际投入金额
            var  zengshishui = $(this).closest('tr').find('input.shizengzhishui:checked').val()
            if(zengshishui==1)
                shijitouru = weishui
            else
                shijitouru = (weishui+shuijin).toFixed(2)

            var td = $(this).closest('tr').find('[name=weishuihexiaojine]').closest('td').prev('td').html()
            td = td.replace(',','')
            td = parseFloat(td)
            if(shijitouru>td){
                swal('','本次核销实际投入金额'+(weishui+shuijin).toFixed(2)+',不能大于未核销金额'+td,'error')
                $(this).val('')
                return false
            }

            $(this).closest('tr').find('[name=shuilv]').val(shuilv)
            $(this).closest('tr').find('[name=hanshuihexiaojine]').val((weishui+shuijin).toFixed(2)) //含税核销金额
            $(this).closest('tr').find('[name=shijitourujine]').val(shijitouru) //实际投入金额

            sumheji(this)
        })

        //输入未税核销金额自动计算税金等字段
        $('[name=weishuihexiaojine]').on('blur',function () {
            var weishui = $(this).val()
            if(!/^\d+(\.\d+)?$/.test(weishui)){
                swal('请输入非负浮点数')
                $(this).val('')
                return false
            }
            weishui = parseFloat(weishui)
            var weihexiao = $(this).closest('td').prev('td').html();
            weihexiao = weihexiao.replace(',','')
            weihexiao = parseFloat(weihexiao)
            var shuilv = $(this).closest('tr').find('[name=shuilv]').val()
            var shuijin = parseFloat((weishui*shuilv/100).toFixed(2))

            var shijitouru  //实际投入金额
            var  zengshishui = $(this).closest('tr').find('input.shizengzhishui:checked').val()
            if(zengshishui==1)
                shijitouru = weishui
            else
                shijitouru = (weishui+shuijin).toFixed(2)

            var td = $(this).closest('tr').find('[name=weishuihexiaojine]').closest('td').prev('td').html()
            td = td.replace(',','')
            td = parseFloat(td)
            if(shijitouru>td){
                swal('','本次核销实际投入金额'+(weishui+shuijin).toFixed(2)+',不能大于未核销金额'+td,'error')
                return false
            }

            if(weishui>weihexiao){
                swal('未税核销金额不能大于未核销金额')
                $(this).val('')
                return false
            }

            $(this).closest('tr').find('[name=shuijin]').val(shuijin)
            $(this).closest('tr').find('[name=hanshuihexiaojine]').val((weishui+shuijin).toFixed(2)) //含税核销金额
            $(this).closest('tr').find('[name=shijitourujine]').val(shijitouru) //实际投入金额

            sumheji(this)
        })

        //设置有误发票
        $('.youwufapiao').on('click',function () {
            var val = $(this).val()
            if(val==0){
                $(this).closest('tr').find('[name=fapiaohao]').val('').prop('readonly',true)
            }else{
                $(this).closest('tr').find('[name=fapiaohao]').prop('readonly',false)
            }
        })
    });

    function back() {
        history.go(-1)
    }
</script>
</body>

</html>